<?php
namespace app\admin\controller\mall;
use app\common\controller\AdminController;
use think\facade\Db;
use think\facade\View;


/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/22 0022
 * Time: 10:24
 */
class Store extends AdminController
{
    public function initialize()
    {
        parent::initialize();
        $this->model = app('app\common\model\mall\Store');
        $this->field='id,name,phone,address,sort,star,status,update_time,is_top,category_id';
    }
    /**
     * 获取附带数据
     * @param Request $request
     * @return mixed
     */
    public function get_other($request)
    {
        $cate = app('app\common\model\common\Categories');
        $tag = app('app\common\model\common\Tags');
        $store_tag = app('app\common\model\common\RelationTag');

        $data['cate'] = [
            ['id'=>16,'name'=>'艺术类'],
            ['id'=>1,'name'=>'文化类'],
            ['id'=>17,'name'=>'早教类'],
        ];
        $data['tag'] = $tag->where([['type','=',1]])->select();
        if(!empty($request['id'])){
            $data['store_tag'] = $store_tag->where([['other_id','=',$request['id']]])->column('tag_id');
        }

        View::assign('data',$data);
    }
    public function action(){

        $param= $this->request->param();

        try{
            Db::name('store')->where('id',$param['id'])->update(['is_top'=>$param['is_top']]);


        }catch (\Exception $exception){
            return json(['code'=>0,'msg'=>$exception->getMessage()]);
        }
        return json(['code'=>1,'msg'=>'操作成功']);
    }


    /**
     * @NodeAnotation(title="编辑")
     */
    public function check()
    {
        $id = $this->request->param('id');
        $row = $this->model->with('StoreData')->find($id);


        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            try {
                $post = $this->request->only(['id','comment','status']);
                $StoreUser = app('app\common\model\system\SystemAdmin');
                //默认新增商家总账号
                //新增默认商家超级用户
                $user_admin = ['nickname'=>$post['name'].'超级管理员','auth_ids'=>3,'app_id'=>$this->model->getApp(),
                    'phone'=>$post['phone'],
                    'username'=>$post['phone'],
                    'password'=>password($post['phone']),
                    'store_id'=>$post['id'],
                    'is_super'=>0,
                    'is_store'=>1
                ];
                if( !$StoreUser->where($user_admin)->find()){
                    $StoreUser->save($user_admin);
                }

                $save = Db::name('store')->where('id',$id)->save($post);
                //$save = $row->save($post);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error($e->getMessage());
        }
        View::assign('row', $row);
        if(method_exists($this,'get_other')){
            $this->get_other($this->request->param() ?? []);
        }
        return view('', compact('row'));
    }

    /**
     * 列表搜索条件
     */
    public function get_where($arr=[]){
        $array=[];
        if(!empty($arr['name'])){
            $array[] = ['name','like','%'.$arr['name'].'%'];
        }
        isset($arr['phone']) && !empty($arr['phone']) && $array[] = ['phone', '=', $arr['phone']];
        isset($arr['status']) && !empty($arr['status']) && $array[] = ['status', '=', $arr['status']];
        return $array;
    }

    /**
     * 获取验证规则
     * @param $post
     * @return array
     */
    public function get_check_rule($post)
    {
        $rule = [
            'star|商家星级' => 'between:0,5',
            'phone|手机号'  => 'max:20|unique:store,phone,'.$post['id'].',id|unique:system_admin,username,'.$post['id'].',store_id',
        ];

        return $rule;
    }

    /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {

        $advertmodel = app('app\common\model\banner\Advert');//广告
        $row = $this->model->whereIn('id', $id)->select();
        empty($row) && $this->error('数据不存在');
        $advert = $advertmodel->where(['type'=>1,'bind_id'=>$id])->find();
        if(!empty($advert)){$this->error('此商家机构已绑定广告，请解绑后在删除');}
        try {
            $save = $row->delete();
        } catch (\Exception $e) {
            $this->error('删除失败');
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }

    public function count(){

        
        dd(strtotime('2020-08-03 00:00:00'));
    }
}